package com.caimore.logger;

import com.caimore.common.SmsUtils_sdk;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

public class LoggerThread extends Thread {

   private volatile boolean m_bExitThread = false;
   private String m_sLogPath;
   private LinkedList m_lstLog;
   private ReentrantLock m_lockLog;
   private Semaphore m_semLog;
   private String m_sLineSeparator;
   private int m_iMaxHandleCount;


   public LoggerThread(String _sLogPath) {
      this.m_sLogPath = _sLogPath;
      this.m_lstLog = new LinkedList();
      this.m_lockLog = new ReentrantLock();
      this.m_semLog = new Semaphore(0);
      this.m_sLineSeparator = System.getProperty("line.separator");
      this.m_iMaxHandleCount = 10;
   }

   public void exitThread() {
      this.m_bExitThread = true;
   }

   public void pushLog(String _sLog) {
      try {
         this.m_lockLog.lock();
         this.m_lstLog.addLast(_sLog);
      } finally {
         this.m_lockLog.unlock();
      }

      this.m_semLog.release();
   }

   public void run() {
      String sLastLogFullFilePath = null;
      FileWriter fw = null;

      while(!this.m_bExitThread) {
         this.m_semLog.acquireUninterruptibly();
         LinkedList lstLog = null;
         int iCount = 0;

         try {
            this.m_lockLog.lock();
            if(!this.m_lstLog.isEmpty()) {
               lstLog = new LinkedList();

               while(!this.m_lstLog.isEmpty()) {
                  lstLog.addLast((String)this.m_lstLog.removeFirst());
                  ++iCount;
                  if(iCount > this.m_iMaxHandleCount) {
                     break;
                  }
               }
            }
         } finally {
            this.m_lockLog.unlock();
         }

         if(iCount > 2) {
            this.m_semLog.acquireUninterruptibly(iCount - 1);
         }

         if(lstLog != null) {
            String sLogFileName = SmsUtils_sdk.getNowDateString() + ".log";
            String sLogFilePath = this.m_sLogPath + SmsUtils_sdk.getNowYMString() + "/";
            File fileLogPath = new File(sLogFilePath);
            if(!fileLogPath.exists()) {
               fileLogPath.mkdirs();
            }

            String sLogFullFilePath = sLogFilePath + sLogFileName;

            try {
               if(!sLogFullFilePath.equalsIgnoreCase(sLastLogFullFilePath)) {
                  if(fw != null) {
                     fw.close();
                  }

                  fw = new FileWriter(sLogFullFilePath, true);
                  sLastLogFullFilePath = sLogFullFilePath;
               }

               if(lstLog != null) {
                  Iterator var10 = lstLog.iterator();

                  while(var10.hasNext()) {
                     String sLog = (String)var10.next();
                     fw.write(sLog);
                     fw.write(this.m_sLineSeparator);
                  }

                  fw.flush();
               }
            } catch (IOException var15) {
               ;
            }
         }
      }

      if(fw != null) {
         try {
            fw.close();
         } catch (IOException var14) {
            ;
         }
      }

   }
}
